package com.lbw.controller.chat;

import com.lbw.chatMemory.repository.MySqlMemoryRepository;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
@RequestMapping("/ollama")
public class OllamaController {

    @Autowired
    @Qualifier("gemma3ChatClient")
    private ChatClient gemma3ChatClient;

    @Autowired
    private MySqlMemoryRepository mySqlMemoryRepository;

    @PostMapping(value = "/chat", produces = "text/html;charset=utf-8")
    public Flux<String> chat(@RequestBody Map<String,String> paramsMap) {
        // 保存会话id
        mySqlMemoryRepository.saveConversationId(paramsMap.get("conversationId"));

        return gemma3ChatClient.prompt()
                .user(paramsMap.get("prompt"))
                .advisors(spec -> spec
                        .param("chat_memory_conversation_id", paramsMap.get("conversationId"))
                )
                .stream()
                .content();
    }

}
